package edu.cmu.cs.cs214.hw9.client.gui;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import edu.cmu.cs.cs214.hw9.client.controllers.Event;
import edu.cmu.cs.cs214.hw9.client.controllers.FacelookEvent;
import edu.cmu.cs.cs214.hw9.datatypes.User;

/**
 * Displays the list of pending and current friends for a particualr user. 
 */
public class FriendsScreen extends FacelookScreen {
	private static final long serialVersionUID = -6580515861259128249L;
	
	/**
	 * Go to a friend's profile when the friend's name has been clicked on. 
	 */
	private class FriendSelector implements ListSelectionListener {
		public void valueChanged(ListSelectionEvent e) {
			JList sourceList = (JList) e.getSource();
			User user = (User) sourceList.getSelectedValue();
			
			if (user != null) {
				fireEventOccured(new FacelookEvent(Event.GO_TO_PROFILE, user)); 
			}
		}
	}

	/**
	 * Constructs a friends page based on the list of pending and current friends of the 
	 * currently logged in user.
	 * 
	 * @param pendingFriends List of pending friends.
	 * @param currentFriends List of current friends.
	 */
	public FriendsScreen(List<User> pendingFriends, List<User> currentFriends) {
		// Layout ini.
		GridBagConstraints c = new GridBagConstraints();
		
		// Newsfeed button goes back to the logged in user's newsfeed.
		JButton newsFeedButton = new JButton("Back to NewsFeed");
		newsFeedButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				fireEventOccured(new FacelookEvent(Event.GO_TO_FEED, null));
			}
		});
		
		// Informative labels
		JLabel pendingFriendsLabel = new JLabel("Pending requests");
		JLabel currentFriendsLabel = new JLabel("My friends");
		
		// Make label font white.
		pendingFriendsLabel.setForeground(Color.white);
		currentFriendsLabel.setForeground(Color.white);
		
		// Generate the list of current & pending friends.
		JList pendingFriendsList = new JList(pendingFriends.toArray(new User[pendingFriends.size()]));
		JList currentFriendsList = new JList(currentFriends.toArray(new User[currentFriends.size()]));
		
		// We use a custom rendered to render friend data in the list. 
		pendingFriendsList.setCellRenderer(new FriendRenderer());
		currentFriendsList.setCellRenderer(new FriendRenderer());
		pendingFriendsList.addListSelectionListener(new FriendSelector());
		currentFriendsList.addListSelectionListener(new FriendSelector());
		
		// Make sure that the lists of friends are scrollable!
		JScrollPane pendingScroller = new JScrollPane(pendingFriendsList);
		JScrollPane currentScroller = new JScrollPane(currentFriendsList);
		pendingScroller.setBackground(new Color(59, 89, 152));
		currentScroller.setBackground(new Color(59, 89, 152));
		
		// Add components to the layout.
		
		// 1st Row
		c.gridx = 0;
		c.gridwidth = 2;
		add(newsFeedButton, c);
		
		// 2nd Row
		c.gridy = 1;
		c.gridwidth = 1;
		add(pendingFriendsLabel, c);
		
		c.gridx = 1;
		add(currentFriendsLabel, c);
		
		// 3rd Row
		c.gridy = GridBagConstraints.RELATIVE;
		c.gridx = 0;
		add(pendingScroller, c);
		
		c.gridx = 1;
		add(currentScroller, c);
		
	}
}
